package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.NoticeAdapter;
import com.dingwei.superiordiary.bean.NoticeBean;
import com.dingwei.superiordiary.mvp.presenter.NoticePresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.L;
import com.lihao.baselibrary.weight.TitleBar;
import com.nikhilpanju.recyclerviewenhanced.RecyclerTouchListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/10/27.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class NoticeActivity extends BaseActivity implements Contract.NoticeView {

    NoticePresenter noticePresenter = new NoticePresenter(this);


    NoticeAdapter adapter;
    String type;
    int page = 1;
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.tv_only_look_unread)
    TextView tvOnlyLookUnread;
    @Bind(R.id.tv_all_read)
    TextView tvAllRead;
    @Bind(R.id.order_tablayout)
    TabLayout orderTablayout;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;
    private RecyclerTouchListener onTouchListener;
    private int state;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_my_notice;
    }

    @Override
    protected void initData() {
        int p =getIntent().getIntExtra("position",0);
        if (p==4){
            orderTablayout.getTabAt(4).select();
            type = "3";
        }else {
            orderTablayout.getTabAt(0).select();
            type = "";
        }

        refreshLayout.autoRefresh();
    }

    @Override
    protected void initView() {

        titleBar.setOnMenuListener(new TitleBar.OnMenuListener() {
            @Override
            public void onMenuClick() {
                intent = new Intent(BaseActivity.context, NotifySetActivity.class);
                startActivity(intent);
            }
        });


        orderTablayout.addTab(orderTablayout.newTab().setText("总览"));
        orderTablayout.addTab(orderTablayout.newTab().setText("评论"));
        orderTablayout.addTab(orderTablayout.newTab().setText("@我"));
        orderTablayout.addTab(orderTablayout.newTab().setText("关注我"));
        orderTablayout.addTab(orderTablayout.newTab().setText("点赞"));

        adapter = new NoticeAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.line), 1));
        onTouchListener = new RecyclerTouchListener(this, mRecyclerView);
        onTouchListener.setClickable(new RecyclerTouchListener.OnRowClickListener() {
            @Override
            public void onRowClicked(int position) {
            }

            @Override
            public void onIndependentViewClicked(int independentViewID, int position) {
            }
        })
                .setSwipeOptionViews(R.id.btnDelete, R.id.btnRead)
                .setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
                    @Override
                    public void onSwipeOptionClicked(int viewID, int position) {
                        if (viewID == R.id.btnDelete) {
                            NoticeBean.DataBean.ListBean bean = adapter.getItem(position);
                            noticePresenter.delete(bean.id, position);
                        } else if (viewID == R.id.btnRead) {
                            NoticeBean.DataBean.ListBean bean = adapter.getItem(position);
                            noticePresenter.read(bean.id, position);
                        }
                    }
                });

        if (getIntent().hasExtra("position")) {
            orderTablayout.getTabAt(getIntent().getIntExtra("position", 0)).select();
            type = "4";
        }


    }

    @Override
    protected void initListener() {

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshLayout) {
                page = page + 1;
                noticePresenter.initData(page, type, state, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                noticePresenter.initData(page, type, state, true, false);
            }
        });

        orderTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                switch (tab.getPosition()) {
                    case 0:
                        type = "";
                        break;
                    case 1:
                        type = "2";
                        break;
                    case 2:
                        type = "1";
                        break;
                    case 3:
                        type = "4";
                        break;
                    case 4:
                        type = "3";
                        break;
                }
                page = 1;
                noticePresenter.initData(page, type, state, true, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        adapter.setOnMarkReadInterfaceListener(new NoticeAdapter.MarkReadInterFace() {
            @Override
            public void nickname(int position) {
                NoticeBean.DataBean.ListBean bean = adapter.getItem(position);
                Intent intent = new Intent(BaseActivity.context, LookUserCenterActivity.class);
                intent.putExtra("user_id", bean.user_id);
                BaseActivity.context.startActivity(intent);
            }

            @Override
            public void ForwardNickName(int position) {
                NoticeBean.DataBean.ListBean bean = adapter.getItem(position);
                Intent intent = new Intent(BaseActivity.context, LookUserCenterActivity.class);
                intent.putExtra("user_id",bean.activity_data.id);
                BaseActivity.context.startActivity(intent);
            }

            @Override
            public void read(int position) {
                NoticeBean.DataBean.ListBean bean = adapter.getItem(position);
                noticePresenter.read(bean.id,position);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mRecyclerView.addOnItemTouchListener(onTouchListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mRecyclerView.removeOnItemTouchListener(onTouchListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_only_look_unread, R.id.tv_all_read, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_only_look_unread:
                if (state == 0) {
                    state = 1;
                    tvOnlyLookUnread.setTextColor(getResources().getColor(R.color.theme_color));
                } else {
                    state = 0;
                    tvOnlyLookUnread.setTextColor(getResources().getColor(R.color.text_color_gray));
                }
                page = 1;
                noticePresenter.initData(page, type, state, true, true);
                break;
            case R.id.tv_all_read:
                noticePresenter.read("", -1);
                break;
            case R.id.no_net_btn:
                page = 1;
                noticePresenter.initData(page, type, state, true, true);
                break;
        }
    }

    @Override
    public void onFail() {

        showData("fail");
    }

    @Override
    public void onRefresh(NoticeBean bean) {
        onTouchListener.closeVisibleBG();

        adapter.clear();
        adapter.notifyDataSetChanged();

        adapter.addAll(bean.data.list);
        adapter.notifyDataSetChanged();

        refreshLayout.finishRefresh();
        showData("success");
    }

    @Override
    public void loadMore(NoticeBean bean) {

        onTouchListener.closeVisibleBG();

        adapter.addAll(bean.data.list);
        adapter.notifyDataSetChanged();
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void readSuccess(int position) {
        if (position != -1) {
            NoticeBean.DataBean.ListBean bean = adapter.getItem(position);
            bean.status = 1;
            adapter.notifyDataSetChanged();
        } else {
            for (NoticeBean.DataBean.ListBean listBean : adapter.getAllData()) {
                listBean.status = 1;
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void deleteSuccess(int position) {
        adapter.remove(position);
    }


    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            if (adapter.getAllData() != null && adapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            refreshLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }
}
